package com.zrrd.jt_admin.shop.category.controller;


import com.zrrd.jt_admin.common.dto.CategorySortDataDto;
import com.zrrd.jt_admin.common.entity.Category;
import com.zrrd.jt_admin.common.response.ResponseResult;
import com.zrrd.jt_admin.common.vo.CategoryVo;
import com.zrrd.jt_admin.shop.category.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Zhangyk
 * @since 2024-07-01
 */
@Api(tags = "商品分类管理-接口文档")
@RestController
@RequestMapping("admin/category")
@ResponseBody
public class CategoryController {

    @Resource
    private CategoryService categoryService;

    @ApiOperation(value = "商品分类列表")
    @GetMapping("/")
    public ResponseResult findAll(){
         CategoryVo categoryVo = categoryService.findAll();
         return ResponseResult.queryResponse(categoryVo);
    }

    @ApiOperation(value = "增加商品分类")
    @PostMapping("/")
    public ResponseResult addCategory(@RequestBody Category category){

        boolean flag = categoryService.save(category);
        return ResponseResult.addResponse(flag);
    }

    @ApiOperation(value = "修改商品分类")
    @PutMapping
    public ResponseResult updateCategory(@RequestBody Category category){
        boolean flag = categoryService.updateById(category);
        return ResponseResult.updateResponse(flag);
    }

    @ApiOperation(value = "修改商品分类状态")
    @PutMapping("{id}/updateStatus`")
    public ResponseResult updateCategoryStatus(@PathVariable("id") Integer id,
                                               Integer status){
        Boolean bStatus=status==1?true:false;
        boolean flag = categoryService.lambdaUpdate().eq(Category::getId, id)
                .set(Category::getStatus, bStatus).update();

        return ResponseResult.updateResponse(flag);
    }

    @ApiOperation(value = "删除商品分类")
    @DeleteMapping("{id}/delete")
    public ResponseResult deleteCategory(@PathVariable("id") Integer id){
        return ResponseResult.removeResponse(categoryService.removeById(id));
    }

    @ApiOperation(value = "拖拽商品分类排序")
    @DeleteMapping("sort")
    public ResponseResult deleteCategorySort(@RequestBody CategorySortDataDto categorySortDataDto){
        boolean flag = categoryService.lambdaUpdate()
                .eq(Category::getId, categorySortDataDto.getId())
                .set(Category::getCategoryId, categorySortDataDto.getCategoryId())
                .set(Category::getOrdered, categorySortDataDto.getOrdered())
                .update();
        return ResponseResult.updateResponse(flag);
    }
}

